import {
  upload2WechatByFileID,
  syncMaterial
} from '@/api/channel'
export default {
  props: {

  },
  data() {
    return {
      syncMaterialTimer: null,
      progressNum: 0,
      syncMaterialLoding: false
    }
  },
  created() {

  },
  computed: {

  },
  methods: {
    uploadFileChange({ currentRow }) {
      upload2WechatByFileID({ ids: currentRow, mode: 'perm' }).then(res => {
        const { data: { code, msg } } = res
        if (code === 1) {
          this.$message.success(msg)
        } else {
          this.$message.error(msg)
        }
        this.getTableList && this.getTableList()
      })
    },
    syncMaterial(callBack, type, pindex = 0) {
      this.syncMaterialLoding = true
      // this.progressNum = 0
      syncMaterial({ type: type, pindex }).then(res => {
        const { data: { code, msg, data: { stop, now_count, total_count, pindex } } } = res
        if (code === 1 && stop === 1) {
          this.$message.success(msg)
        }
        callBack && callBack(stop, now_count, total_count, pindex)
      })
    },
    syncMaterialCallBack(stop, now_count, total_count, pindex) {
      const progressNum = parseFloat((now_count / total_count * 100).toFixed(2))
      this.progressNum = progressNum > 100 ? 100 : progressNum
      if (stop !== 1) {
        this.syncMaterial(this.syncMaterialCallBack, 'image', pindex)
      } else {
        clearTimeout(this.syncMaterialTimer)
        this.syncMaterialTimer = setTimeout(() => {
          this.syncMaterialLoding = false
          this.progressNum = 0
          this.getTableList && this.getTableList()
        }, 1000)
      }
    }
  }
}

